//
//  ASFeedBackController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASFeedBackController.h"
#import "ASUserApi.h"

#define PUSH_BTN_SIZE  CGSizeMake(296, 38)

@interface ASFeedBackController () <YYTextViewDelegate>

@property (strong, nonatomic) YYTextView *textView;
@property (strong, nonatomic) UIView *textBackground;

@property (strong, nonatomic) YYTextView *qqText;
@property (strong, nonatomic) UIView *qqTextBackground;

@property (strong, nonatomic) UIButton *pushBtn;
@property (strong, nonatomic) UIView *shadowView;

@end

@implementation ASFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.title = @"意见反馈";
    
    [self.view addSubview:self.textBackground];
    [self.view addSubview:self.qqTextBackground];
    [self.textBackground addSubview:self.textView];
    [self.qqTextBackground addSubview:self.qqText];
    [self.view addSubview:self.pushBtn];
    [self.view addSubview:self.shadowView];
    [self setAllViewLayout];
    // Do any additional setup after loading the view.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textView resignFirstResponder];
    [self.qqText resignFirstResponder];
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.textBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(14);
        make.top.mas_equalTo(Height_NavBar + 16);
        make.right.mas_equalTo(-14);
        make.height.mas_equalTo(120);
    }];
    
    [self.qqTextBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.mas_equalTo(14);
        make.top.mas_equalTo(self.textBackground.mas_bottom).mas_offset(15);
        make.right.mas_equalTo(-14);
        make.height.mas_equalTo(46);
    }];
    
    [self.pushBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(PUSH_BTN_SIZE);
        make.top.mas_equalTo(self.qqTextBackground.mas_bottom).mas_offset(45);
    }];
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(PUSH_BTN_SIZE);
        make.top.mas_equalTo(self.qqTextBackground.mas_bottom).mas_offset(45);
    }];
    
}

- (UIView *)textBackground{
    if (!_textBackground) {
        _textBackground = [UIView new];
        _textBackground.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _textBackground.layer.masksToBounds = YES;
        _textBackground.layer.cornerRadius = 6;
        _textBackground.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
        _textBackground.layer.borderWidth = 1.f;
    }
    return _textBackground;
}

- (UIView *)qqTextBackground{
    if (!_qqTextBackground) {
        _qqTextBackground = [UIView new];
        _qqTextBackground.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _qqTextBackground.layer.masksToBounds = YES;
        _qqTextBackground.layer.cornerRadius = 6;
        _qqTextBackground.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
        _qqTextBackground.layer.borderWidth = 1.f;
    }
    return _qqTextBackground;
}

-(YYTextView *)textView{
    if (!_textView) {
        _textView = [[YYTextView alloc]initWithFrame:CGRectMake(14, 10, kScreenWidth - (28 * 2), 120 - 20)];
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.textColor = [UIColor colorWithHexString:@"1a1a1a"];
        _textView.placeholderFont = [UIFont systemFontOfSize:15];
        _textView.placeholderTextColor = [UIColor colorWithHexString:@"#CCCCCC"];
        _textView.placeholderText = @"请详细描述你的问题或建议~";
        _textView.delegate = self;
    }
    return _textView;
}

-(YYTextView *)qqText{
    if (!_qqText) {
        _qqText = [[YYTextView alloc]initWithFrame:CGRectMake(14,  8, kScreenWidth - (28 * 2), 30)];
        _qqText.font = [UIFont systemFontOfSize:15];
        _qqText.textColor = [UIColor colorWithHexString:@"1a1a1a"];
        _qqText.placeholderFont = [UIFont systemFontOfSize:15];
        _qqText.placeholderTextColor = [UIColor colorWithHexString:@"#CCCCCC"];
        _qqText.placeholderText = @"输入正确的QQ号(必填)";
        _qqText.scrollEnabled = NO;
        _qqText.keyboardType = UIKeyboardTypeNumberPad;
        _qqText.delegate = self;
    }
    return _qqText;
}

- (UIButton *)pushBtn{
    if (!_pushBtn) {
        _pushBtn = [UIButton new];
        [_pushBtn setTitle:@"提交" forState:UIControlStateNormal];
        _pushBtn.layer.masksToBounds = YES;
        _pushBtn.layer.cornerRadius = 19;
        [_pushBtn addTarget:self action:@selector(clickPushBtn) forControlEvents:UIControlEventTouchUpInside];
        [_pushBtn setAdjustsImageWhenHighlighted:NO];
        [_pushBtn setBackgroundColor:[UIColor colorWithHexString:@"#FF7785"]];
        [_pushBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
        _pushBtn.titleLabel.font = [UIFont systemFontOfSize:19];
        
//        UIImage *image2  = [DFTool colorGradientWithStartColor:[UIColor colorWithHexString:@"#b3b3b3"] endColor:[UIColor colorWithHexString:@"#dbdbdb"] ifVertical:NO imageSize:CGSizeMake(154, 36)];
//        [_pushBtn setBackgroundImage:image2 forState:UIControlStateNormal];
//        [_pushBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
    }
    return _pushBtn;
}

- (UIView *)shadowView{
    if (!_shadowView) {
        _shadowView = [UIView new];
        _shadowView.backgroundColor = [UIColor lightGrayColor];
        _shadowView.alpha = 0.3;
        _shadowView.layer.masksToBounds = YES;
        _shadowView.layer.cornerRadius = 19;
        _shadowView.userInteractionEnabled = YES;
        _shadowView.hidden = YES;
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            NSString *contentStr = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString *qqStr = [self.qqText.text stringByReplacingOccurrencesOfString:@" " withString:@""];

            if (qqStr.length < 5 || qqStr.length > 13) {
                [MBProgressHUD showError:@"请输入正确的联系方式"];
                return;
            }else if(contentStr.length < 2){
                [MBProgressHUD showError:@"内容不能少于2个字"];
                return;
            }
        }];
        [_shadowView addGestureRecognizer:tap];
    }
    return _shadowView;
}

#pragma mark - YYTextViewDelegate
- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(YYTextView *)textView{
    NSString *contentStr = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *qqStr = [self.qqText.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if (qqStr.length >= 5 && contentStr.length >= 2 && qqStr.length <= 13) {
//        self.shadowView.hidden = YES;
//        _pushBtn.selected = YES;
    }else{
//        self.shadowView.hidden = NO;
//        _pushBtn.selected = NO;
    }
}

- (void)clickPushBtn{
    NSString *contentStr = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *qqStr = [self.qqText.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if (qqStr.length < 5 || qqStr.length > 13) {
        [MBProgressHUD showError:@"请输入正确的联系方式"];
        return;
    }else if(contentStr.length < 2){
        [MBProgressHUD showError:@"内容不能少于2个字"];
        return;
    }
    
    [MBProgressHUD showMessage:@"提交中"];
    [ASUserApi feedbackWithContent:self.textView.text qqStr:self.qqText.text Completion:^(ASBaseModel *  _Nonnull result) {
        [MBProgressHUD hideHUD];
        if (result.state.intValue == 1) {
            [MBProgressHUD showSuccess:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showError:result.errorMsg];
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:FENETERROR];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
